package cn.mygweb.admin.modules.web.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mygweb.admin.modules.web.pojo.Columns;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;
import cn.mygweb.common.pojo.RedisKey;

/** 
* <p>Title: ColumnsController.java</p>
* <p>Description: 栏目管理</p>  
* @author gxb 
* @date 2019年6月26日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Controller
@RequestMapping("/admin/web/columns")
public class ColumnsController extends BaseController{

	/**
	 * <p>Title: columnList</p>
	 * <p>Description: 栏目管理列表页面</p>
	 * @return
	 */
	@RequestMapping("/list")
	public String columnList() {
		return "admin/modules/web/columns";
	}
	
	/**
	 * <p>Title: ajaxColumnsList</p>
	 * <p>Description: 异步获取栏目列表</p>
	 * @return
	 */
	@RequestMapping("/ajaxColumnsList")
	@ResponseBody
	public JsonResult ajaxColumnsList() {
		List<Columns> list  = columnsService.queryListByExample(new Columns());
		return JsonResult.ok(list);
	}
	
	/**
	 * <p>Title: ajaxColumnsSelectNode</p>
	 * <p>Description: 返回select下拉选类型的节点元素</p>
	 * @return
	 */
	@RequestMapping("/ajaxColumnsSelectNode")
	@ResponseBody
	public JsonResult ajaxColumnsSelectNode() {
		List<Columns> list  = columnsService.queryListByExample(new Columns());
		StringBuilder builder = new StringBuilder();
		builder.append("<select name='columnName'>");
		for (Columns columns : list) {
			builder.append("<option value='"+columns.getId()+"'>"+columns.getName()+"</option>");
		}
		builder.append("</select>");
		return JsonResult.build(200, "", builder.toString());
	}
	
	/**
	 * <p>Title: operation</p>
	 * <p>Description: 栏目增、删、改操作</p>
	 * @param columns
	 * @param oper
	 * @return
	 */
	@RequestMapping("/operation")
	@ResponseBody
	public JsonResult operation(Columns columns, String oper) {
		if("add".equals(oper)) {//添加操作
			try {
				String id = sid.nextShort();
				columns.setId(id);
				columnsService.insert(columns);
				super.delRedisCacheByKeys(RedisKey.ADMIN_WEB_CONTENT, RedisKey.KEY_WEB_INDEX);
				return JsonResult.ok("添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("添加成功");
			}
		} else if("edit".equals(oper)) {//修改操作
			try {
				columnsService.updateByPrimaryKeySelective(columns);
				super.delRedisCacheByKeys(RedisKey.ADMIN_WEB_CONTENT, RedisKey.KEY_WEB_INDEX);
				return JsonResult.ok("修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("修改成功");
			}
		} else if("del".equals(oper)) {//删除操作
			try {
				String ids = columns.getId();
				String[] idArr = ids.split(",");
				columnsService.delete(idArr);//批量删除
				super.delRedisCacheByKeys(RedisKey.ADMIN_WEB_CONTENT, RedisKey.KEY_WEB_INDEX);
				return JsonResult.ok("删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("删除失败");
			}
		} else {
			return null;
		}
	}
}
